package com.hspedu.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/1/24
 * @time 18:07
 */
@SuppressWarnings("all")
public class SmallChangeOOP {
    boolean flag = true;
    Scanner scan = new Scanner(System.in);
    //零钱通明细
    String detail = "-----------------零钱通明细---------------";

    //收益入账
    double money = 0;
    double balance = 0;
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    //消费
    String note = null;

    public void detail() {
        System.out.println(detail);
    }

    public void income() {
        while (true) {
            System.out.println("请输入收益入账金额：");
            money = scan.nextDouble();
            if (money > 0) {
                balance += money;
                date = new Date();
                detail += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
                return;
            }
            System.out.println("输入不合法,请重新输入，收益应当大于0");
        }
    }

    public void outcome() {
        if (balance == 0) {
            System.out.println("现在余额为0，不能消费");
            return;
        }

        while (true){
            System.out.print("消费金额：");
            money = scan.nextDouble();
            if (money < balance && money > 0){
                balance -= money;
                System.out.print("消费说明：");
                note = scan.next();
                date = new Date();
                detail += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                return;
            }
            if (money > balance) {
                System.out.println("消费超过余额，请重新输入");
            } else if (money <= 0) {
                System.out.println("输入非法，消费金额应该为正数");
            }
        }

    }

    public void exit() {
        boolean flag4 = true;
        do {
            System.out.println("确认要退出程序？y/n");
            char a = scan.next().charAt(0);
            if (a == 'y') {
                flag = false;
                flag4 = false;
                System.out.println("退出程序");
            } else if (a == 'n') {
                flag4 = false;
                System.out.println("回到首页");
            } else {
                System.out.println("输入不合法，请重新输入");
            }
        } while (flag4);
    }

    public void mainMenu() {
       do {
           System.out.println("\n-----------零钱通菜单-----------");
           System.out.println("\t\t\t1 零钱通明细");
           System.out.println("\t\t\t2 收益入账");
           System.out.println("\t\t\t3 消\t费");
           System.out.println("\t\t\t4 退\t出");
           System.out.println("请选择1-4：");
           char key = scan.next().charAt(0);

           switch (key) {
               case '1':
                   this.detail();
                   break;
               case '2':
                   this.income();
                   break;
               case '3':
                   this.outcome();
                   break;
               case '4':
                   this.exit();
                   break;
               default:
                   System.out.println("输入有误请重新输入");
                   break;
           }
       }while (flag);
    }
}
